package cn.six1943.deepchat.module.ai.controller.admin.model;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.six1943.deepchat.module.ai.controller.admin.model.vo.*;
import cn.six1943.deepchat.module.ai.dal.dataobject.model.ProviderInstDO;
import cn.six1943.deepchat.module.ai.dal.dataobject.model.ModelDO;
import cn.six1943.deepchat.module.ai.service.model.ProviderInstOffcService;

@Tag(name = "管理后台 - 模型密钥")
@RestController
@RequestMapping("/ai/provider-inst")
@Validated
public class ProviderInstController {

    @Resource
    private ProviderInstOffcService providerInstOffcService;

    @PostMapping("/create")
    @Operation(summary = "创建模型密钥")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:create')")
    public CommonResult<Long> createProviderInst(@Valid @RequestBody ProviderInstSaveReqVO createReqVO) {
        return success(providerInstOffcService.createProviderInst(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型密钥")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:update')")
    public CommonResult<Boolean> updateProviderInst(@Valid @RequestBody ProviderInstSaveReqVO updateReqVO) {
        providerInstOffcService.updateProviderInst(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型密钥")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:delete')")
    public CommonResult<Boolean> deleteProviderInst(@RequestParam("id") Long id) {
        providerInstOffcService.deleteProviderInst(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型密钥")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:query')")
    public CommonResult<ProviderInstRespVO> getProviderInst(@RequestParam("id") Long id) {
        ProviderInstDO providerInst = providerInstOffcService.getProviderInst(id);
        return success(BeanUtils.toBean(providerInst, ProviderInstRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型密钥分页")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:query')")
    public CommonResult<PageResult<ProviderInstRespVO>> getProviderInstPage(@Valid ProviderInstPageReqVO pageReqVO) {
        PageResult<ProviderInstDO> pageResult = providerInstOffcService.getProviderInstPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProviderInstRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型密钥 Excel")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProviderInstExcel(@Valid ProviderInstPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProviderInstDO> list = providerInstOffcService.getProviderInstPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模型密钥.xls", "数据", ProviderInstRespVO.class,
                        BeanUtils.toBean(list, ProviderInstRespVO.class));
    }

    // ==================== 子表（模型） ====================

    @GetMapping("/model/list-by-key-id")
    @Operation(summary = "获得模型列表")
    @Parameter(name = "keyId", description = "提供商密钥")
    @PreAuthorize("@ss.hasPermission('ai:provider-api-key:query')")
    public CommonResult<List<ModelDO>> getModelListByKeyId(@RequestParam("keyId") Long keyId) {
        return success(providerInstOffcService.getModelListByKeyId(keyId));
    }

}